package tk.mybatis.springboot.service.impl;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.springboot.mapper.AccessConnectMapper;
import tk.mybatis.springboot.mapper.AccessPurviewMapper;
import tk.mybatis.springboot.mapper.AccessRoleMapper;
import tk.mybatis.springboot.mapper.AccessVisitMapper;
import tk.mybatis.springboot.mapper.CharmTypeMapper;
import tk.mybatis.springboot.mapper.CommodityMapper;
import tk.mybatis.springboot.mapper.LeavingmessageMapper;
import tk.mybatis.springboot.mapper.OrderHistoryMapper;
import tk.mybatis.springboot.mapper.OrderMapper;
import tk.mybatis.springboot.mapper.UserMapper;
import tk.mybatis.springboot.mapper.UserMessageMapper;
import tk.mybatis.springboot.model.AccessConnect;
import tk.mybatis.springboot.model.AccessPurview;
import tk.mybatis.springboot.model.AccessRole;
import tk.mybatis.springboot.model.AccessVisit;
import tk.mybatis.springboot.model.AdminVo;
import tk.mybatis.springboot.model.CharmType;
import tk.mybatis.springboot.model.Commodity;
import tk.mybatis.springboot.model.Leavingmessage;
import tk.mybatis.springboot.model.Message;
import tk.mybatis.springboot.model.OrderHistory;
import tk.mybatis.springboot.model.TOrder;
import tk.mybatis.springboot.model.TUser;
import tk.mybatis.springboot.model.Usermessage;
import tk.mybatis.springboot.service.AdminService;
import tk.mybatis.springboot.share.util.MD5Utils;
import tk.mybatis.springboot.share.util.RedisUtils;
import tk.mybatis.springboot.share.util.StringUtils;
import tk.mybatis.springboot.share.util.UniqId;
@Service
@Transactional
public class AdminServiceImpl implements AdminService{
	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private CharmTypeMapper charmTypeMapper;
	
	@Autowired
	private CommodityMapper commodityMapper;
	
	@Autowired
	private LeavingmessageMapper leavingmessageMapper;
	
	@Autowired
	private OrderMapper orderMapper;
	
	@Autowired
	private OrderHistoryMapper orderHistoryMapper;
	
	@Autowired
	private AccessRoleMapper accessRoleMapper;
	
	@Autowired
	private UserMessageMapper userMessageMapper;
	
	@Autowired
	private AccessConnectMapper accessConnectMapper;
	
	@Autowired
	private AccessPurviewMapper accessPurviewMapper;
	
	@Autowired
	private AccessVisitMapper accessVisitMapper;
	@Override
	public Message login(String sessionId, String phone, String pwd) {
		AdminVo userByPhone = userMapper.getUserByPhone(phone);
		if(userByPhone == null){
			return Message.error("用户不存在，请检查手机号输入是否正确");
		}
		if(!StringUtils.toLowerCaseAll(MD5Utils.toMd5(pwd)).equals(userByPhone.getPassword())){
			return Message.error("密码错误，请检查手机号或者密码输入是否正确");
		};
		userByPhone.setPassword("");
		RedisUtils.setObj(sessionId, userByPhone);
		return Message.success(userByPhone);
	}
	@Override
	public Message clerkFind(TUser tUser, Integer pageNumber, Integer pageSize) {
		tUser.setMtype("counterman");
		Integer count = userMapper.select(tUser).size();
		pageNumber = pageNumber == null ? 0 : pageNumber;
		pageSize = pageSize == null ? 10 : pageSize;
		PageHelper.startPage(pageNumber, pageSize);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("count" ,count);
		map.put("list", userMapper.select(tUser));
		return Message.success(map);
	}
	@Override
	public Message clerkAdd(TUser tUser) {
		tUser.setMtype("counterman");
		List<TUser> select = userMapper.select(tUser);
		if(select != null && select.size() > 0){
			return Message.error("该手机号已被注册");
		}
		tUser.setId(UniqId.getUid());
		tUser.setState("0");
		tUser.setCreatedate(new Date());
		tUser.setModifydate(new Date());
		tUser.setStatus("0");
		userMapper.insertSelective(tUser);
		return Message.success("添加成功");
	}
	@Override
	public Message clerkEdit(TUser tUser) {
		tUser.setModifydate(new Date());
		userMapper.updateByPrimaryKeySelective(tUser);
		return Message.success("修改成功");
	}
	@Override
	public Message clerkStatus(String id, String status) {
		Example example=new Example(TUser.class);
		String[] values = id.split(",");
		List<String> asList = Arrays.asList(values);
		example.createCriteria().andIn("id",asList);
		TUser tUser = new TUser();
		tUser.setStatus(status);
		userMapper.updateByExampleSelective(tUser, example);
		return Message.success("修改成功");
	}
	@Override
	public Message charmtypeFind() {
		List<CharmType> selectAll = charmTypeMapper.selectAll();
		return Message.success("查询成功", selectAll);
	}
	@Override
	public Message charmtypeAdd(String name) {
		CharmType charmType = new CharmType();
		charmType.setName(name);
		charmType.setId(UniqId.getUid());
		charmTypeMapper.insertSelective(charmType);
		return Message.success("添加成功");
	}
	@Override
	public Message charmtypeEdit(String id, String name) {
		CharmType charmType = new CharmType();
		charmType.setName(name);
		charmType.setId(id);
		charmTypeMapper.updateByPrimaryKeySelective(charmType);
		return Message.success("修改成功");
	}
	@Override
	public Message charmtypeDel(String id) {
		TUser tUser = new TUser();
		tUser.setCharmTypeId(id);
		List<TUser> select = userMapper.select(tUser);
		if(select == null || select.isEmpty()){
			return Message.error("存在业务员引用此魅力类型，暂时不能删除");
		}
		CharmType charmType = new CharmType();
		charmType.setId(id);
		charmTypeMapper.deleteByPrimaryKey(charmType);
		return Message.success("删除成功");
	}
	@Override
	public Message clerkDel(String id) {
		TUser tUser = new TUser();
		tUser.setCharmTypeId(id);
		tUser.setState("9");
		userMapper.updateByPrimaryKeySelective(tUser);
		return Message.success("删除成功");
	}
	@Override
	public Message goodsFind(Commodity commodity, Integer pageNumber, Integer pageSize) {
		List<Commodity> select = commodityMapper.select(commodity);
		Integer count = select.size();
		pageNumber = pageNumber == null ? 0 : pageNumber;
		pageSize = pageSize == null ? 10 : pageSize;
		PageHelper.startPage(pageNumber, pageSize);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("count" ,count);
		for(int x=0; x< select.size(); x++){
			if(select.get(x).getState() == 9){
				select.remove(x);
				x--;
			}
		}
		map.put("list", select);
		return Message.success(map);
	}
	@Override
	public Message goodsAdd(String name, String price) {
		Commodity commodity = new Commodity();
		commodity.setCreatedate(new Date());
		commodity.setModifydate(new Date());
		commodity.setName(name);
		commodity.setState(0);
		commodity.setPrice(new BigDecimal(price));
		commodity.setId(UniqId.getUid());
		commodityMapper.insertSelective(commodity);
		return Message.success("添加成功");
	}
	@Override
	public Message goodsEdit(Commodity commodity) {
		commodity.setModifydate(new Date());
		commodityMapper.updateByPrimaryKeySelective(commodity);
		return Message.success("修改成功");
	}
	@Override
	public Message goodsDel(String id) {
		Example example=new Example(Commodity.class);
		String[] values = id.split(",");
		List<String> asList = Arrays.asList(values);
		example.createCriteria().andIn("id",asList);
		Commodity commodity = new Commodity();
		commodity.setState(9);
		commodityMapper.updateByExampleSelective(commodity, example);
		return Message.success("删除成功");
	}
	@Override
	public Message goodsStatus(String id, Integer status) {
		Example example=new Example(Commodity.class);
		String[] values = id.split(",");
		List<String> asList = Arrays.asList(values);
		example.createCriteria().andIn("id",asList);
		Commodity commodity = new Commodity();
		commodity.setState(status);
		commodityMapper.updateByExampleSelective(commodity, example);
		return Message.success("修改成功");
	}
	@Override
	public Message userFind(TUser tUser, Integer pageNumber, Integer pageSize) {
		Integer count = userMapper.select(tUser).size();
		pageNumber = pageNumber == null ? 0 : pageNumber;
		pageSize = pageSize == null ? 10 : pageSize;
		PageHelper.startPage(pageNumber, pageSize);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("count" ,count);
		map.put("list", userMapper.select(tUser));
		return Message.success(map);
	}
	@Override
	public Message userStatus(String id, String status) {
		Example example=new Example(TUser.class);
		String[] values = id.split(",");
		List<String> asList = Arrays.asList(values);
		example.createCriteria().andIn("id",asList);
		TUser tUser = new TUser();
		tUser.setState(status);
		userMapper.updateByExampleSelective(tUser, example);
		return Message.success("修改成功");
	}
	@Override
	public Message userAccess(String userId, String accessId) {
		TUser tUser = userMapper.selectByPrimaryKey(userId);
		tUser.setAccessId(accessId);
		userMapper.updateByPrimaryKeySelective(tUser);
		return Message.success("分配成功");
	}
	@Override
	public Message commentFind(String userName, String context, Date contextDateBegin, Date contextDateEnd,
			Integer pageNumber, Integer pageSize, Integer status) {
		Integer count = leavingmessageMapper.findLeavingmessageList(userName, context, contextDateBegin, contextDateEnd, status).size();
		pageNumber = pageNumber == null ? 0 : pageNumber;
		pageSize = pageSize == null ? 10 : pageSize;
		PageHelper.startPage(pageNumber, pageSize);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("count" ,count);
		map.put("list", leavingmessageMapper.findLeavingmessageList(userName, context, contextDateBegin, contextDateEnd, status));
		return Message.success(map);
	}
	@Override
	public Message commentReply(String id, String reply) {
		Leavingmessage leavingmessage = leavingmessageMapper.selectByPrimaryKey(id);
		leavingmessage.setReply(reply);
		leavingmessage.setReplyDate(new Date());
		leavingmessageMapper.updateByPrimaryKeySelective(leavingmessage);
		return Message.success("回复成功");
	}
	@Override
	public Message commentStatus(String id, Integer status) {
		Example example=new Example(Leavingmessage.class);
		String[] values = id.split(",");
		List<String> asList = Arrays.asList(values);
		example.createCriteria().andIn("id",asList);
		Leavingmessage leavingmessage = new Leavingmessage();
		leavingmessage.setStatus(status);
		leavingmessageMapper.updateByExampleSelective(leavingmessage, example);
		return Message.success("修改成功");
	}
	@Override
	public Message commentDel(String id) {
		Example example=new Example(Leavingmessage.class);
		String[] values = id.split(",");
		List<String> asList = Arrays.asList(values);
		example.createCriteria().andIn("id",asList);
		Leavingmessage leavingmessage = new Leavingmessage();
		leavingmessage.setStatus(9);
		leavingmessageMapper.updateByExampleSelective(leavingmessage, example);
		return Message.success("删除成功");
	}
	@Override
	public Message managerPwd(String id, String pwdNew, String pwdOld) {
		TUser tUser = userMapper.selectByPrimaryKey(id);
		if(!StringUtils.toLowerCaseAll(MD5Utils.toMd5(pwdOld)).equals(tUser.getPassword())){
			return Message.error("密码错误，请重新输入密码");
		};
		tUser.setPassword(StringUtils.toLowerCaseAll(MD5Utils.toMd5(pwdNew)));
		tUser.setModifydate(new Date());
		userMapper.updateByPrimaryKeySelective(tUser);
		return Message.success("修改成功");
	}
	@Override
	public Message managerAdd(TUser tUser) {
		AdminVo userByPhone = userMapper.getUserByPhone(tUser.getPhone());
		if(userByPhone == null){
			return Message.error("该手机已经注册，不能重复注册");
		}
		tUser.setMtype("system");
		tUser.setId(UniqId.getUid());
		tUser.setCreatedate(new Date());
		tUser.setModifydate(new Date());
		tUser.setState("0");
		userMapper.insertSelective(tUser);
		return Message.success("添加成功");
	}
	@Override
	public Message orderFind(String orderNo, String phone, Date createDateBegin, Date createDateEnd,
			Integer pageNumber, Integer pageSize, String address) {
		pageNumber = pageNumber == null ? 0 : pageNumber;
		pageSize = pageSize == null ? 10 : pageSize;
		PageHelper.startPage(pageNumber, pageSize);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("count" ,orderMapper.findOrderCount(orderNo, phone, createDateBegin, createDateEnd, address));
		map.put("list", orderMapper.findOrderList(orderNo, phone, createDateBegin, createDateEnd, address));
		return Message.success(map);
	}
	@Override
	public Message orderStatus(String id, Integer status) {
		TOrder tOrder = new TOrder();
		tOrder.setId(id);
		tOrder.setState(status);
		orderMapper.updateByPrimaryKeySelective(tOrder);
		OrderHistory orderHistory = new OrderHistory();
		orderHistory.setId(UniqId.getUid());
		orderHistory.setCreate_date(new Date());
		orderHistory.setOrder_id(id);
		orderHistory.setStatus(status);
		orderHistoryMapper.insertSelective(orderHistory);
		return Message.success("修改成功");
	}
	@Override
	public Message orderHistory(String id) {
		return Message.success(orderHistoryMapper.findOrderHistory(id));
	}
	@Override
	public Message notificationFind(Usermessage userMessage, Integer pageNumber, Integer pageSize) {
		List<Usermessage> select = userMessageMapper.select(userMessage);
		Integer count = select.size();
		pageNumber = pageNumber == null ? 0 : pageNumber;
		pageSize = pageSize == null ? 10 : pageSize;
		PageHelper.startPage(pageNumber, pageSize);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("count" ,count);
		userMessageMapper.select(userMessage);
		for(int x=0; x< select.size(); x++){
			if(select.get(x).getStatus() == 9){
				select.remove(x);
				x--;
			}
		}
		map.put("list", select);
		return Message.success(map);
	}
	@Override
	public Message notificationAdd(Usermessage userMessage) {
		userMessage.setCreatedate(new Date());
		userMessage.setType("system");
		userMessage.setId(UniqId.getUid());
		userMessageMapper.insertSelective(userMessage);
		return Message.success("发布成功");
	}
	@Override
	public Message notificationEdit(Usermessage userMessage) {
		userMessageMapper.updateByPrimaryKeySelective(userMessage);
		return Message.success("修改成功");
	}
	@Override
	public Message notificationDel(String id) {
		Usermessage userMessage = userMessageMapper.selectByPrimaryKey(id);
		userMessage.setStatus(9);
		userMessageMapper.updateByPrimaryKeySelective(userMessage);
		return Message.success("删除成功");
	}
	@Override
	public Message accessRolefind(String title, String abbreviation, Integer pageNumber, Integer pageSize) {
		AccessRole accessRole = new AccessRole();
		accessRole.setAbbreviation(abbreviation);
		accessRole.setTitle(title);
		Integer count = accessRoleMapper.select(accessRole).size();
		pageNumber = pageNumber == null ? 0 : pageNumber;
		pageSize = pageSize == null ? 10 : pageSize;
		PageHelper.startPage(pageNumber, pageSize);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("count" ,count);
		map.put("list", accessRoleMapper.select(accessRole));
		return Message.success(map);
	}
	@Override
	public Message accessRoleadd(String title, String abbreviation) {
		AccessRole accessRole = new AccessRole();
		accessRole.setAbbreviation(abbreviation);
		accessRole.setTitle(title);
		accessRole.setId(UniqId.getUid());
		accessRole.setStatus("0");
		accessRoleMapper.insertSelective(accessRole);
		return Message.success("添加成功");
	}
	@Override
	public Message accessRoledel(String id) {
		accessRoleMapper.deleteByPrimaryKey(id);
		return Message.success("删除成功");
	}
	@Override
	public Message accessRoleedit(AccessRole accessRole) {
		accessRoleMapper.updateByPrimaryKeySelective(accessRole);
		return Message.success("编辑成功");
	}
	@Override
	public Message accessRolestatus(String id, String status) {
		Example example=new Example(AccessRole.class);
		String[] values = id.split(",");
		List<String> asList = Arrays.asList(values);
		example.createCriteria().andIn("id",asList);
		AccessRole accessRole = new AccessRole();
		accessRole.setStatus(status);
		accessRoleMapper.updateByExampleSelective(accessRole, example);
		return Message.success("修改成功");
	}
	@Override
	public Message accessAddrole(List<AccessConnect> accessConnects) {
		for (AccessConnect accessConnect : accessConnects) {
			accessConnectMapper.insertSelective(accessConnect);
		}
		return Message.success("添加成功");
	}
	@Override
	public Message accessPurviewfind(AccessPurview accessPurview, Integer pageNumber, Integer pageSize) {
		Integer count = accessPurviewMapper.select(accessPurview).size();
		pageNumber = pageNumber == null ? 0 : pageNumber;
		pageSize = pageSize == null ? 10 : pageSize;
		PageHelper.startPage(pageNumber, pageSize);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("count" ,count);
		map.put("list", accessPurviewMapper.select(accessPurview));
		return Message.success(map);
	}
	@Override
	public Message accessPurviewadd(AccessPurview accessPurview) {
		accessPurview.setId(UniqId.getUid());
		accessPurview.setStatus("0");
		accessPurviewMapper.insertSelective(accessPurview);
		return Message.success("添加成功");
	}
	@Override
	public Message accessPurviewdel(String id) {
		accessPurviewMapper.deleteByPrimaryKey(id);
		return Message.success("删除成功");
	}
	@Override
	public Message accessPurviewedit(AccessPurview accessPurview) {
		accessPurviewMapper.updateByPrimaryKeySelective(accessPurview);
		return Message.success("修改成功");
	}
	@Override
	public Message accessVisittree() {
		return Message.success(accessVisitMapper.findAll());
	}
	@Override
	public Message accessVisitadd(AccessVisit accessVisit) {
		accessVisit.setStatus("0");
		accessVisit.setId(UniqId.getUid());
		accessVisitMapper.insertSelective(accessVisit);
		return Message.success("添加成功");
	}
	@Override
	public Message accessVisitdel(String id) {
		accessVisitMapper.deleteByPrimaryKey(id);
		return Message.success("删除成功");
	}
	@Override
	public Message accessVisitstatus(String id, String status) {
		Example example=new Example(AccessVisit.class);
		String[] values = id.split(",");
		List<String> asList = Arrays.asList(values);
		example.createCriteria().andIn("id",asList);
		AccessVisit accessVisit = new AccessVisit();
		accessVisit.setStatus(status);
		accessVisitMapper.updateByExampleSelective(accessVisit, example);
		return Message.success("修改成功");
	}
	@Override
	public Message accessVisitedit(AccessVisit accessVisit) {
		accessVisitMapper.updateByPrimaryKeySelective(accessVisit);
		return Message.success("修改成功");
	}
	@Override
	public Message imgupload(MultipartFile img) {
		DiskFileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload sfu = new ServletFileUpload(factory);  
		sfu.setSizeMax(10 * 1024 * 1024);
		 sfu.setHeaderEncoding("utf-8");  
		return null;
	}
}
